package com.easy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.Order;
import com.easy.common.service.IOrderService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Autowired
    IOrderService orderService;

    // 根据id获取订单信息
    @GetMapping("/id/{id}")
    public Result getOrderById(@PathVariable Integer id) {
        return Result.success(orderService.getOrderById(id));
    }

    // 根据用户id获取订单信息
    @GetMapping("/userid/{userId}")
    public Result getOrderByUserId(@PathVariable Integer userId) {
        return Result.success(orderService.getOrderByUserId(userId));
    }

    // 添加订单
    @PostMapping("/add")
    public Result addOrder(@RequestBody Order order) {
        int result = orderService.addOrder(order);
        return result > 0 ? Result.success("添加成功") : Result.fail(500, "添加失败");
    }

    // 更新订单
    @PutMapping("/update")
    public Result updateOrder(@RequestBody Order order) {
        int result = orderService.updateOrder(order);
        return result > 0 ? Result.success("更新成功") : Result.fail(500, "更新失败");
    }

    // 删除订单（软删除）
    @DeleteMapping("/delete/{id}")
    public Result deleteOrder(@PathVariable Integer id) {
        int result = orderService.deleteOrder(id);
        return result > 0 ? Result.success("删除成功") : Result.fail(500, "删除失败");
    }

    // 获取订单列表
    @GetMapping("/list")
    public Result getOrderList(Order order) {
        return Result.success(orderService.getAll(order));
    }

    // 获取订单分页列表
    @GetMapping("/page")
    public Result getOrderPage(Order order, Integer current, Integer size) {
        IPage<Order> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size);
        return Result.success(orderService.getOrderPage(order, page));
    }
}
